﻿using System;
using System.Collections;
using System.Data;
using ZYCommon;

namespace ZYCommon
{
    

    public abstract class ZYDBRecordBase
    {
        protected bool bolUpdateSuccess = false;
        protected DataRowState intDataState = DataRowState.Added;
       
        protected ZYDBRecordBase()
        {
        }

        public virtual ZYDBRecordBase CreateNewRecord()
        {
            return null;
        }

        public virtual bool CustomDelete(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool FromReader(IDataReader myReader, ZYDBConnection conn)
        {
            return false;
        }

        ////新增
        //public virtual bool FromDataSet(DataSet myDs, IDbConnection conn)
        //{
        //}

        //获得删除记录的总数
        public static int GetDeletedRecordCount(ArrayList myList)
        {
            int iCount = 0;
            foreach (ZYDBRecordBase r in myList)
            {
                if (r.isDeleted())
                {
                    iCount++;
                }
            }
            return iCount;
        }

        //获得表名
        public virtual string GetTableName()
        {
            return null;
        }

        //判断是否被删除
        public bool isDeleted()
        {
            return (this.intDataState == DataRowState.Deleted);
        }

        public virtual bool isKeyEnable()
        {
            return false;
        }

        //判断是否被修改过
        public bool isModified()
        {
            return (this.intDataState == DataRowState.Modified);
        }

        //判断是否新纪录
        public bool isNewRecord()
        {
            return (this.intDataState == DataRowState.Added);
        }


        public bool isUnchanged()
        {
            return (this.intDataState == DataRowState.Unchanged);
        }

        //删除记录 
        public static void RemoveDeletedRecord(ArrayList myList, bool CheckSuccess)
        {
            if ((myList != null) && (myList.Count > 0))
            {
                for (int iCount = myList.Count - 1; iCount >= 0; iCount--)
                {
                    ZYDBRecordBase r = (ZYDBRecordBase) myList[iCount];
                    if (r.isDeleted() && (!CheckSuccess || r.UpdateSuccess))
                    {
                        myList.RemoveAt(iCount);
                    }
                }
            }
        }

        //设置查询最大seq(相当于id)的sql语句 并返回值
        public virtual bool SetAllocKeyCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            if ((myCmd != null) && (this.GetTableName() != null))
            {
                myCmd.CommandText = "Select Max(RecordSEQ) From SEQTable Where TableName = '" + this.GetTableName() + "'";
                myCmd.Parameters.Clear();
                return true;
            }
            return false;
        }


        public virtual bool SetDeleteCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetInsertCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetKeyWord(string strKey)
        {
            return false;
        }

        public virtual void SetModified()
        {
            if ((this.intDataState != DataRowState.Added) && (this.intDataState != DataRowState.Deleted))
            {
                this.intDataState = DataRowState.Modified;
            }
        }

        public virtual bool SetSelectAllCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetSelectCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetSelectCommandForOneRecord(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetTestExistCommand(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual bool SetUpdateCommnad(IDbCommand myCmd, ZYDBConnection conn)
        {
            return false;
        }

        public virtual DataSet GetRecordDs(IDbCommand myCmd, ZYDBConnection conn)
        {
            //String str = this.strObjectID;

            //string Sql = "Select ObjectID , ObjectName , ObjectType  , ObjectData From ET_Document Where ObjectID =  '" + this.strObjectID + "'"; //"select * from ET_DOCUMENT Where ObjectID = ?";//
            //return App.GetDataSet(Sql);
            ////myCmd.Parameters.Clear();
            ////ZYDBConnection.AddParameter(myCmd, this.strObjectID);
            ////return ds;
            return null;
        }

        public DataRowState DataState
        {
            get
            {
                return this.intDataState;
            }
            set
            {
                this.intDataState = value;
            }
        }

        public bool UpdateSuccess
        {
            get
            {
                return this.bolUpdateSuccess;
            }
            set
            {
                this.bolUpdateSuccess = value;
            }
        }
    }
}

